//
//  BottomBarView.m
//  OfflineSDKDemo
//
//  Created by Lyy on 15/8/18.
//  Copyright (c) 2015年 Gensee, Inc. All rights reserved.
//

#import "BottomBarView.h"



@interface BottomBarView ()
{
    UILabel *_hadTimeLabel;
    UISlider *_slider;
    UILabel *_totalTimeLabel;
    
}

@property (nonatomic ,strong)UIButton *playButton;
@property (nonatomic ,strong)UIButton *fullScreenButton;
@property (nonatomic ,assign)BOOL isPlaying;

@end

@implementation BottomBarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.isPlaying = NO;
        self.layer.masksToBounds = YES;
        [self createSubView];
    }
    return self;
}

- (void)createSubView {
    
    _hadTimeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _hadTimeLabel.textColor = [UIColor whiteColor];
    _hadTimeLabel.font = [UIFont systemFontOfSize:12];
    _hadTimeLabel.textAlignment = NSTextAlignmentRight;
    _hadTimeLabel.text = @"00:00";
    [self addSubview:_hadTimeLabel];
    
    _slider = [[UISlider alloc] initWithFrame:CGRectZero];
    _slider.continuous = NO;
    [_slider addTarget:self action:@selector(changePosition:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_slider];
    
    _totalTimeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _totalTimeLabel.textColor = [UIColor whiteColor];
    _totalTimeLabel.font = [UIFont systemFontOfSize:12];
    _totalTimeLabel.textAlignment = NSTextAlignmentLeft;
    _totalTimeLabel.text = @"/00:00";
    [self addSubview:_totalTimeLabel];

    _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_playButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [_playButton setImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
    [_playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    _playButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    _playButton.backgroundColor = [UIColor redColor];
    [self addSubview:_playButton];
    
    _fullScreenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_fullScreenButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [_fullScreenButton setImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
    [_fullScreenButton addTarget:self action:@selector(fullScreenAction:) forControlEvents:UIControlEventTouchUpInside];
    _fullScreenButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    _fullScreenButton.backgroundColor = [UIColor redColor];
    [self addSubview:_fullScreenButton];
}

#pragma mark - UISlide Action
- (void)changePosition:(UISlider *)slider {
    
    if ([self.delegate respondsToSelector:@selector(bottomBarView:slidePosition:)]) {
        [self.delegate bottomBarView:self slidePosition:slider.value];
    }
    
    _hadTimeLabel.text = [self playTimeFormat:slider.value];
}

#pragma mark -
#pragma mark - button Action
//播放暂停
- (void)playAction:(UIButton *)sender {
    self.isPlaying = !self.isPlaying;
    if ([self.delegate respondsToSelector:@selector(bottomBarView:isPlaying:)]) {
        [self.delegate bottomBarView:self isPlaying:self.isPlaying];
        
        NSString *imageNamed = self.isPlaying ? @"play" : @"Parse";
        [sender setImage:[UIImage imageNamed:imageNamed] forState:UIControlStateNormal];
        [sender setImage:[UIImage imageNamed:imageNamed] forState:UIControlStateHighlighted];
    }
}
// 全屏
- (void)fullScreenAction:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(bottomBarViewFullScreen)]) {
        [self.delegate bottomBarViewFullScreen];
    }
}

- (void)setTotalTime:(int)totalTime {
    
    _totalTime = totalTime;
    self.hidden = !totalTime;
    _slider.maximumValue = totalTime;
    _slider.minimumValue = 0;
    if (_totalTime) {
        NSString *totalTime = [self playTimeFormat:_totalTime];
        _totalTimeLabel.text = [NSString stringWithFormat:@"/%@",totalTime];
    }
}

- (void)setHadPlayTime:(int)hadPlayTime {
    _slider.value = hadPlayTime;
    _hadTimeLabel.text = [self playTimeFormat:hadPlayTime];

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.playButton setFrame:CGRectMake(10, (CGRectGetHeight(self.frame)-30)/2.0, 30, 30)];
    [self.fullScreenButton setFrame:CGRectMake(CGRectGetWidth(self.frame)-30-10, (CGRectGetHeight(self.frame)-30)/2.0, 30, 30)];
     _slider.frame = CGRectMake(CGRectGetMaxX(self.playButton.frame)+10, (CGRectGetHeight(self.frame)-20)/2.0, CGRectGetWidth(self.frame)-CGRectGetMaxX(self.playButton.frame)-30-CGRectGetWidth(self.fullScreenButton.frame), 20);
    
    UIFont *font = [UIFont systemFontOfSize:12];
    
    if (_totalTime) {
        NSString *totalTime = [self playTimeFormat:_totalTime];
        _totalTimeLabel.text = [NSString stringWithFormat:@"/%@",totalTime];
    }
    CGSize totalTimeLabelSize = [_totalTimeLabel.text sizeWithAttributes:@{NSFontAttributeName:font}];
    _totalTimeLabel.frame = CGRectMake(CGRectGetMaxX(_slider.frame)-totalTimeLabelSize.width, CGRectGetMaxY(_slider.frame)-10, totalTimeLabelSize.width, totalTimeLabelSize.height);
    
    CGSize hadTimeLabelSize = [_hadTimeLabel.text sizeWithAttributes:@{NSFontAttributeName:font}];
    _hadTimeLabel.frame = CGRectMake(CGRectGetMinX(_totalTimeLabel.frame)-hadTimeLabelSize.width-10, CGRectGetMaxY(_slider.frame)-10, hadTimeLabelSize.width+10, totalTimeLabelSize.height);
}

//时间格式转换成视频播放显示的时间格式
- (NSString *)playTimeFormat:(CGFloat)timeNumber {
    
    NSString *playTimeString;
    timeNumber = timeNumber/1000;
    if (timeNumber > 60) {
        
        NSInteger minute = timeNumber/60;
        NSString *minuteString = [self formatTime:minute];
        
        NSInteger second = timeNumber - 60*minute;
        NSString *secondString = [self formatTime:second];
        
        playTimeString = [NSString stringWithFormat:@"%@:%@",minuteString,secondString];
    } else {
        
        NSString *minuteString = [self formatTime:timeNumber];
        
        playTimeString = [NSString stringWithFormat:@"%@:%@",@"00",minuteString];
        
    }
    
    return playTimeString;
}

- (NSString *)formatTime:(NSInteger)number {
    
    NSString *timeString = @"";
    if (number < 10) {
        timeString = [NSString stringWithFormat:@"0%zd",number];
    } else {
        timeString = [NSString stringWithFormat:@"%zd",number];
    }
    
    return timeString;
}

@end
